package com.shop.cereshop.app.controller.canvas;

import com.shop.cereshop.app.page.canvas.CanvasCoupon;
import com.shop.cereshop.app.page.canvas.CanvasProduct;
import com.shop.cereshop.app.page.product.ProductCoupon;
import com.shop.cereshop.app.param.canvas.CanvasAppProductParam;
import com.shop.cereshop.app.param.canvas.CanvasCouponParam;
import com.shop.cereshop.app.service.activity.CerePlatformActivityService;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.coupon.CereShopCouponService;
import com.shop.cereshop.app.service.index.IndexService;
import com.shop.cereshop.app.service.product.CereProductClassifyService;
import com.shop.cereshop.app.service.product.CereShopProductService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.canvas.CerePlatformCanvas;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.product.Classify;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 画布选择商品查询
 */
@RestController
@RequestMapping("canvas")
@CrossOrigin
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "CanvasController")
@Api(value = "画布选择商品查询", tags = "画布选择商品查询")
public class CanvasController {

    @Autowired
    private CereShopProductService cereShopProductService;

    @Autowired
    private CereProductClassifyService cereProductClassifyService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private CerePlatformActivityService cerePlatformActivityService;

    @Autowired
    private CereShopCouponService cereShopCouponService;

    @Autowired
    private CereBuyerUserService cereBuyerUserService;

    /**
     * 画布选择商品查询
     *
     * @param param
     * @return
     */
    @GetMapping("getProducts")
    @ApiOperation(value = "商家编辑查询")
    public Result<Page<CanvasProduct>> getCanvasProducts(CanvasAppProductParam param) throws CoBusinessException, Exception {
        Page page = cereShopProductService.getCanvasProducts(param);
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    /**
     * 查询分类层级
     *
     * @return
     */
    @GetMapping("getClassify")
    @ApiOperation(value = "查询分类层级")
    public Result<Classify> getClassify() throws CoBusinessException {
        List<Classify> list = cereShopProductService.getClassify();
        return new Result(list, CoReturnFormat.SUCCESS);
    }

    /**
     * 查询所有平台优惠券
     *
     * @return
     */
    @GetMapping("getCoupons")
    @ApiOperation(value = "查询所有平台优惠券")
    public Result<Page<CanvasCoupon>> getCoupons(CanvasCouponParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Page page = cerePlatformActivityService.getCoupons(param, user);
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    /**
     * 查询所有店铺优惠券
     *
     * @return
     */
    @GetMapping("getShopCoupons")
    @ApiOperation(value = "查询所有店铺优惠券")
    public Result<Page<ProductCoupon>> getShopCoupons(CanvasCouponParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Page page = cereShopCouponService.getShopCoupons(param, user);
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    /**
     * 读取画布数据
     *
     * @return
     */
    @GetMapping("getCanvas")
    @ApiOperation(value = "读取画布数据")
    public Result getCanvas(CerePlatformCanvas canvas, HttpServletRequest request) throws CoBusinessException {
        canvas = indexService.getCanvas(canvas);
        return new Result(canvas, CoReturnFormat.SUCCESS);
    }


}
